package can.bishe.modules.exam.service.impl;

import can.bishe.modules.exam.service.ExamTestpaperService;
import can.bishe.modules.sys.entity.SysDeptEntity;
import can.bishe.modules.sys.service.SysDeptService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import can.bishe.common.utils.PageUtils;
import can.bishe.common.utils.Query;

import can.bishe.modules.exam.dao.ExamInfoDao;
import can.bishe.modules.exam.entity.ExamInfoEntity;
import can.bishe.modules.exam.service.ExamInfoService;


@Service("examInfoService")
public class ExamInfoServiceImpl extends ServiceImpl<ExamInfoDao, ExamInfoEntity> implements ExamInfoService {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private ExamTestpaperService examTestpaperService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ExamInfoEntity> page = this.page(
                new Query<ExamInfoEntity>().getPage(params),
                new QueryWrapper<ExamInfoEntity>()
        );
        for(ExamInfoEntity examInfoOne:page.getRecords()){
            String deptName = sysDeptService.getById(examInfoOne.getExamDept()).getName();
            String tpName = examTestpaperService.getById(examInfoOne.getExamTpid()).getTpName();
            examInfoOne.setDeptName(deptName);
            examInfoOne.setTpName(tpName);
        }

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryWaitExamPage(Map<String, Object> params,Long userId,Long deptId) {

        // 组织下是否要考试
        List<Long> parentDeptIdList = new ArrayList<>();
        SysDeptEntity sysDeptEntity = sysDeptService.getById(deptId);
        Long parentId = sysDeptEntity.getParentId();
        Long ids = deptId;
        int outIndex = 0;
        while (!parentId.equals(0L)&&outIndex < 10) {
            SysDeptEntity sysDeptOne = sysDeptService.getById(ids);
            parentDeptIdList.add(sysDeptOne.getDeptId());
            ids = sysDeptOne.getParentId();
            parentId = ids;
            outIndex += 1;
        }
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String nowTime = dateFormat.format(now);
        IPage<ExamInfoEntity> page = this.page(
                new Query<ExamInfoEntity>().getPage(params),
                new QueryWrapper<ExamInfoEntity>()
                        .in("exam_dept",parentDeptIdList)
                        .apply(StringUtils.isNotBlank(nowTime),
                                "now() > exam_time AND DATE_FORMAT(NOW(),\"%Y%m%d %H:%m:%s\") < DATE_FORMAT(DATE_ADD( exam_time, INTERVAL 1 HOUR ),\"%Y%m%d %H:%m:%s\")",
                                nowTime
                        )
        );

        return new PageUtils(page);
    }

}
